package cn.afactory.tygz.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.common.DirConstants;
import cn.afactory.tygz.nav.NavGuideActivity;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.R;

/**
 * Created with Android Studio.
 * Title:SampleNavActivityDemo
 * Description:
 * Copyright:Copyright (c) 2014
 * Company:中国平安健康保险有限公司
 * Author:Alice
 * Date:15/8/26
 * Time:下午12:20
 * Version 1.0
 */
public class SampleNavActivityDemo extends BaseActivity {
    private Activity mContext;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.sample_nav_activity);

        mContext = this;

        if (initDirs() ) {
            initNavi();
        }
    }

    private static final String APP_FOLDER_NAME = DirConstants.DIR_WORK;
    private String mSDCardPath = null;
    private boolean initDirs() {
        mSDCardPath = getSdcardDir();
        if ( mSDCardPath == null ) {
            return false;
        }
        File f = new File(mSDCardPath, APP_FOLDER_NAME);
        if ( !f.exists() ) {
            try {
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(
                Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }

    String authinfo = null;
    private void initNavi() {
        BaiduNaviManager.getInstance().setNativeLibraryPath(mSDCardPath + "/BaiduNaviSDK_SO");
        BaiduNaviManager.getInstance().init(this, mSDCardPath, APP_FOLDER_NAME,
                new BaiduNaviManager.NaviInitListener() {
                    @Override
                    public void onAuthResult(int status, String msg) {
                        if (0 == status) {
                            authinfo = "key校验成功!";
                        } else {
                            authinfo = "key校验失败, " + msg;
                        }
                        mContext.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                LocalUtils.showToast(mContext, authinfo);
                            }
                        });

                    }

                    public void initSuccess() {
                        LocalUtils.showToast(mContext, "百度导航引擎初始化成功");
                    }

                    public void initStart() {
                        LocalUtils.showToast(mContext, "百度导航引擎初始化开始");
                    }

                    public void initFailed() {
                        LocalUtils.showToast(mContext, "百度导航引擎初始化失败");
                    }
                }, null /*mTTSCallback*/);
    }

    public void onStartNavClick(View v){
        BNRoutePlanNode sNode = null;
        BNRoutePlanNode eNode = null;
        sNode = new BNRoutePlanNode(116.30142, 40.05087,
                "百度大厦", null, BNRoutePlanNode.CoordinateType.GCJ02);
        eNode = new BNRoutePlanNode(116.39750, 39.90882,
                "北京天安门", null, BNRoutePlanNode.CoordinateType.GCJ02);
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new NavRoutePlanListener(sNode));
        }
    }

    public class NavRoutePlanListener implements BaiduNaviManager.RoutePlanListener {

        private BNRoutePlanNode mBNRoutePlanNode = null;
        public NavRoutePlanListener(BNRoutePlanNode node){
            mBNRoutePlanNode = node;
        }

        @Override
        public void onJumpToNavigator() {
            Intent intent = new Intent(SampleNavActivityDemo.this, NavGuideActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(NavGuideActivity.ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        @Override
        public void onRoutePlanFailed() {
            // TODO Auto-generated method stub

        }
    }

}
